package cn.com.shopec.erp.stock.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.stock.dao.IStockOutPurInfoDao;
import cn.com.shopec.erp.stock.entity.StockOutPurInfo;
import cn.com.shopec.erp.stock.vo.StockOutPurInfoVo;

/**
 * @ClassName: StockOutPurInfoDaoImpl.java
 * @Description: 出库商品采购单关系Dao实现类
 * @author: zengxuezhou
 * @date: 2017年1月17日 下午5:36:01
 */
@Repository
public class StockOutPurInfoDaoImpl extends HibernateEntityDao<StockOutPurInfo> implements IStockOutPurInfoDao {

	@Override
	public void deleteStockOutPurInfo(StockOutPurInfo stockOutPurInfo) {
		String hql = "delete from StockOutPurInfo as p where p.stockOutProductId = ?";
		getSession().createQuery(hql).setString(0, stockOutPurInfo.getStockOutProductId()).executeUpdate();
	}

	@Override
	public int getCountByStockNo(StockOutPurInfoVo stockOutPurInfo) {
		DetachedCriteria detachedCriteria = createCriteria();
		if (!StringUtil.isEmpty(stockOutPurInfo.getStockOutTypeNo())) {
			detachedCriteria.add(Restrictions.eq("stockOutTypeNo", stockOutPurInfo.getStockOutTypeNo()));
		}
		if (!StringUtil.isEmpty(stockOutPurInfo.getProdNo())) {
			detachedCriteria.add(Restrictions.eq("prodNo", stockOutPurInfo.getProdNo()));
		}
		if (!StringUtil.isEmpty(stockOutPurInfo.getStockOutProductId())) {
			detachedCriteria.add(Restrictions.eq("stockOutProductId", stockOutPurInfo.getStockOutProductId()));
		}
		return getCountByCriteria(detachedCriteria);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<StockOutPurInfoVo> getListByStockNo(StockOutPurInfoVo stockOutPurInfo, int startOfPage, int pageSize) {
		String sql = "select " + "a.pur_no purNo," + "a.prod_No prodNo," + "b.commoName," + "b.whName," + "b.supName,"
				+ "a.stockout_quantity AS stockoutQuantity " + "from tbl_stockout_purinfo a "
				+ "left join tbl_stock_purchase b on a.stock_pur_id = b.id where 1 = 1 ";
		if (!StringUtil.isEmpty(stockOutPurInfo.getStockOutTypeNo())) {
			sql += "and a.stockout_type_no = '" + stockOutPurInfo.getStockOutTypeNo() + "'";
		}
		if (!StringUtil.isEmpty(stockOutPurInfo.getProdNo())) {
			sql += "and a.prod_no = '" + stockOutPurInfo.getProdNo() + "'";
		}
		if (!StringUtil.isEmpty(stockOutPurInfo.getStockOutProductId())) {
			sql += "and a.stockout_product_id = '" + stockOutPurInfo.getStockOutProductId() + "'";
		}
		Query query = getSession().createSQLQuery(sql).addScalar("purNo", StandardBasicTypes.STRING)
				.addScalar("prodNo", StandardBasicTypes.STRING).addScalar("commoName", StandardBasicTypes.STRING)
				.addScalar("supName", StandardBasicTypes.STRING).addScalar("whName", StandardBasicTypes.STRING)
				.addScalar("stockoutQuantity", StandardBasicTypes.LONG)
				.setResultTransformer(Transformers.aliasToBean(StockOutPurInfoVo.class));
		List<StockOutPurInfoVo> stockHistoryVo = query.list();
		return stockHistoryVo;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Object[]> queryForStockOut(String stockOutProductId) {
		String sql = "SELECT pur_no,stockout_quantity FROM tbl_stockout_purinfo WHERE STOCKOUT_PRODUCT_ID = ?";
		Query query = getSession().createSQLQuery(sql).setString(0, stockOutProductId);
		return query.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Object[]> queryForStockIn(String stockInProductId) {
		String sql = "SELECT pur_no,stockout_quantity FROM tbl_stockout_purinfo WHERE STOCKIN_PRODUCT_ID = ?";
		Query query = getSession().createSQLQuery(sql).setString(0, stockInProductId);
		return query.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Object[]> queryForStockTransfer(String transferNo, String prodNo) {
		String sql = "SELECT pur_no,stockout_quantity FROM tbl_stockout_purinfo WHERE PROD_NO = ? and STOCKOUT_TYPE_NO = ?";
		Query query = getSession().createSQLQuery(sql).setString(0, prodNo).setString(1, transferNo);
		return query.list();
	}

}
